-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthenticationController.java
More file actions
50 lines (39 loc) · 1.88 KB
/
AuthenticationController.java
File metadata and controls
50 lines (39 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.example.catfactsdaily.controller;
import com.example.catfactsdaily.dto.UserDTO;
import com.example.catfactsdaily.entity.User;
import com.example.catfactsdaily.response.LoginResponse;
import com.example.catfactsdaily.response.SignupResponse;
import com.example.catfactsdaily.service.AuthenticationService;
import com.example.catfactsdaily.service.JwtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@CrossOrigin
@RequestMapping("/auth")
@RestController
public class AuthenticationController {
private final JwtService jwtService;
private final AuthenticationService authenticationService;
@Autowired
public AuthenticationController(JwtService jwtService, AuthenticationService authenticationService) {
this.jwtService = jwtService;
this.authenticationService = authenticationService;
}
@PostMapping("/signup")
public ResponseEntity<SignupResponse> register(@RequestBody UserDTO registerUserDto) {
User registeredUser = authenticationService.signup(registerUserDto);
SignupResponse signupResponse = new SignupResponse();
signupResponse.setName(registeredUser.getName());
return ResponseEntity.ok(signupResponse);
}
@PostMapping("/login")
public ResponseEntity<LoginResponse> authenticate(@RequestBody UserDTO loginUserDto) {
User authenticatedUser = authenticationService.authenticate(loginUserDto);
String jwtToken = jwtService.generateToken(authenticatedUser, authenticatedUser.getUserId());
LoginResponse loginResponse = new LoginResponse();
loginResponse.setName(authenticatedUser.getName());
loginResponse.setToken(jwtToken);
loginResponse.setExpiresIn(jwtService.getExpirationTime());
return ResponseEntity.ok(loginResponse);
}
}